/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    int sumOfNode(TreeNode* root)
    {
        if (nullptr == root)
            return 0;
        return root->val + sumOfNode(root->left) + sumOfNode(root->right);
    }
public:
    int findTilt(TreeNode* root) {
        if (nullptr == root)
            return 0;
        if (nullptr == root->left && nullptr == root->right)
            return 0;
        int val_left = sumOfNode(root->left);
        int val_right = sumOfNode(root->right);
        int ret = val_left > val_right ? val_left - val_right : val_right - val_left;
        return ret + findTilt(root->left) + findTilt(root->right);
    }
};
